﻿using System;
using System.Collections.Generic;
using System.Text;

namespace DotNetExtensions.Core.Restful
{
    public class ApiResult
    {
        public int Code { get; set; }
        public string Msg { get; set; }
        public object Data { get; set; }

        public ApiResult()
        {

        }

        public ApiResult(int code, string msg, object data)
        {
            Code = code;
            Msg = msg;
            Data = data;
        }
        public static ApiResult OK()
        {
            return new ApiResult(200, "The operation was executed successfully", null);
        }

        public static ApiResult OK(object data)
        {
            return new ApiResult(200, "The operation was executed successfully", data);
        }
        public static ApiResult<T> OK<T>(T data)
        {
            return new ApiResult<T>(200, "The operation was executed successfully", data);
        }

        public static ApiResult OK(string msg, object data)
        {
            return new ApiResult(200, msg, data);
        }

        public static ApiResult<T> OK<T>(string msg, T data)
        {
            return new ApiResult<T>(200, msg, data);
        }

        public static ApiResult Failed()
        {
            return new ApiResult(500, "Operation execution failed", null);
        }

        public static ApiResult Failed(string msg)
        {
            return new ApiResult(500, msg, null);
        }
        public static ApiResult<T> Failed<T>(T data)
        {
            return new ApiResult<T>(500, "Operation execution failed", data);
        }
        public static ApiResult Failed(string msg, object data)
        {
            return new ApiResult(500, msg, data);
        }
        public static ApiResult<T> Failed<T>(string msg, T data)
        {
            return new ApiResult<T>(500, msg, data);
        }
        public static ApiResult OK(bool ret)
        {
            if (ret == false) return new ApiResult(500, "Operation execution failed", null);
            return new ApiResult(200, "The operation was executed successfully", null);
        }
        
        public static ApiResult<T> OK<T>(bool ret, T data)
        {
            if (ret == false) return new ApiResult<T>(500, "Operation execution failed", data);
            return new ApiResult<T>(200, "The operation was executed successfully", data);
        }
        public static ApiResult OK(int count)
        {
            if (count == 0) return new ApiResult(500, "Operation execution failed", null);
            return new ApiResult(200, "The operation was executed successfully", null);
        }
    }

    public class ApiResult<T> : ApiResult
    {
        public ApiResult() : base()
        {

        }
        public ApiResult(int code, string msg, T data) : base(code, msg, data)
        {
            Data = data;
        }
        public new T Data { get; set; }
    }
}
